package ${cfg.package_name}.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ${cfg.package_name}.dtomapper.${entity}DtoMapper;
import ${cfg.package_name}.dto.${entity}Dto;
import ${cfg.package_name}.bean.${entity};
import ${cfg.package_name}.mapper.${entity}Mapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.service.BaseV5ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.io.Serializable;

/**
 * ${table.comment!} 服务实现类
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Service(value = "${table.entityPath}Service")
public class ${entity}Service extends BaseV5ServiceImpl<${entity}Mapper, ${entity}, ${entity}Dto> {

    @Resource
    private ${entity}DtoMapper ${table.entityPath}DtoMapper;

    /**
    * create a object
    * @param dto
    * @return
    */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Result<${entity}Dto> create(${entity}Dto dto) {
        ${entity} bean = ${table.entityPath}DtoMapper.dto2bean(dto);
        super.baseMapper.insert(bean);
        return Result.builder(bean);
    }


    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Result<${entity}Dto> saveBatch(List<${entity}Dto> dtoList) {
        List<${entity}> beanList = ${table.entityPath}DtoMapper.dto2beanList(dtoList);
        super.saveBatch(beanList);
        return Result.builder();
    }

    /**
    * update a object by Id
    * 乐观锁：会自动判断和更新version
    * @param dto
    * @return
    */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Result<${entity}Dto> updateByDtoId(${entity}Dto dto) {
        // dto to entity
        ${entity} bean = ${table.entityPath}DtoMapper.dto2bean(dto);
        ${entity} bean2 = super.baseMapper.selectById(dto.getId());
        BeanUtils.copyProperties(bean, bean2, "id","version");
        super.baseMapper.updateById(bean2);
        return Result.builder();
    }

    /**
    * update a object by Id
    * 无乐观锁：不会判断和更新version
    * @param dto
    * @param updateWrapper
    * @return
    */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Result<${entity}Dto> update(${entity}Dto dto, UpdateWrapper updateWrapper) {
        ${entity} bean = ${table.entityPath}DtoMapper.dto2bean(dto);
        super.baseMapper.update(bean, updateWrapper);
        return Result.builder();
    }


    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Transactional(readOnly = true)
    public Result<${entity}Dto> selectBy(QueryWrapper<${entity}> queryWrapper) {
        List<${entity}> list = super.baseMapper.selectList(queryWrapper);
        if (list==null) {
        return Result.builder();
        }
        List<${entity}Dto> dtoList = ${table.entityPath}DtoMapper.bean2dtoList(list);
        return Result.builder(dtoList);
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Transactional(readOnly = true)
    public Result<${entity}Dto> selectById(Serializable id) {
        ${entity} bean = super.baseMapper.selectById(id);
        if (bean==null) {
            //return Result.builder().fault("数据不存在");
            /*
            判断方法
            if (result.getObj()==null) {
            return Result.builder().fault("指定的ID数据不存在");
            }*/
            return Result.builder();
        }
        ${entity}Dto dto = ${table.entityPath}DtoMapper.bean2dto(bean);
        return Result.builder(dto);
    }



    /**
    * select dto list by key
    * @param columnName
    * @param key
    * @return
    */
    @Transactional(readOnly = true)
    public Result<${entity}Dto> selectDtoListByKey(String columnName, String key) {
        Result<${entity}Dto> result = new Result<>();

        List<${entity}> beanList = selectListByKey(columnName, key);
        if (beanList == null) {
            return result.fault("no data by key:"+key);
        }
        List<${entity}Dto> dtoList = ${table.entityPath}DtoMapper.bean2dtoList(beanList);

        return Result.builder(dtoList);
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Transactional(readOnly = true)
    public Result<${entity}Dto> myPage(Page<${entity}> page, QueryWrapper<${entity}> queryWrapper) {
        return this.myPage(page, queryWrapper, true);
    }

    @Transactional(readOnly = true)
    public Result<${entity}Dto> myPage(Page<${entity}> page, QueryWrapper<${entity}> queryWrapper, Boolean hasFooter) {
        Result<${entity}Dto> result = new Result();

        IPage<${entity}> iPage = super.baseMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<${entity}Dto> dtoList = ${table.entityPath}DtoMapper.bean2dtoList(iPage.getRecords());
        // 返回结果集
        result.getMap().put("total", (int)iPage.getTotal());
        result.getMap().put("rows", dtoList);

        // 合计
        if (hasFooter!=null && hasFooter.booleanValue()) {
            List<${entity}> sumList = super.baseMapper.mySelectSum(queryWrapper);
            List<${entity}Dto> sumDtoList = ${table.entityPath}DtoMapper.bean2dtoList(sumList);
            result.getMap().put("footer", sumDtoList);
        }

        return result.success();
    }

    /**
    * 查询
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Transactional(readOnly = true)
    public Result<${entity}Dto> mySelectBy(QueryWrapper<${entity}> queryWrapper) {
        Result<${entity}Dto> result = new Result();

        List<${entity}> list = super.baseMapper.mySelectBy(queryWrapper);
        List<${entity}Dto> dtoList = ${table.entityPath}DtoMapper.bean2dtoList(list);
        result.setList(dtoList);

        return result.success();
    }


    @Transactional(readOnly = true)
    public Result<${entity}Dto> mySelectById(Serializable id) {
        Result<${entity}Dto> result = new Result();

        ${entity} bean = super.baseMapper.mySelectById(id);
        ${entity}Dto dto = ${table.entityPath}DtoMapper.bean2dto(bean);;
        result.setObj(dto);

        return result.success();
    }



}
